<?php
require_once '../class/config.php';
include_once('modules/tuvi/models/TuviModel.php');

define('PER_PAGE', 12);
class TuviController extends Controller {

    public function index() {
        $this->listTuvi();
    }

    public function insert() {
        if (isset($_POST['submit'])) {
            $db = new HomeModel();
            $params = array();
            $params[] = 'ssss';
            $params[] = $_POST['ages'];
            $params[] = $_POST['contents'];
            $params[] = $_POST['years'];
            $cm = new Common();
            $params[] = $cm->uploadImage($_FILES['file']);
            $result = $db->insert($params);
            if ($result == true) {
                $this->data['notice'] = 'success';
                $this->data['act_insert'] = 'Thêm mới thành công !';
            } else {
                $this->data['notice'] = 'error';
                $this->data['act_insert'] = 'Thêm mới không thành công !';
            }
            $this->listTuvi();
        } else {
            $this->data['ages'] = '';
            $this->data['contents'] = '';
            $this->data['years'] = '';
            $this->data['act_title'] = 'Thêm mới';
            $this->data['act'] = 'insert';
            $this->data['character'] = 'Vova';
            return $this->view('admin/tuvi/form', $this->data);
        }
    }

    public function edit() {
        if (isset($_POST['submit']) && $_POST['id'] > 0) {
            $db = new HomeModel();
            $params = array();
            $params[] = 'ssssi';
            $params[] = $_POST['ages'];
            $params[] = $_POST['contents'];
            $params[] = $_POST['years'];

            $image = '';
            if (isset($_FILES['file']['tmp_name']) && $_FILES['file']['tmp_name'] != '') {
                $cm = new Common();
                $image = $cm->uploadImage($_FILES['file']);
            } else{
                if(isset($_POST['image']) && $_POST['image'] != '') {
                    $image = $_POST['image'];
                }
            }
            $params[] = $image;

            $params[] = $_POST['id'];

            $result = $db->editTuvi($params);
            if ($result == true) {
                $this->data['notice'] = 'success';
                $this->data['act_insert'] = 'Thay đổi thành công !';
            } else {
                $this->data['notice'] = 'error';
                $this->data['act_insert'] = 'Thay đổi không thành công !';
            }
            $this->listTuvi();
        } else {
            $db = new HomeModel();
            $id = $_GET['id'];
            $result = $db->getTuviById($id);
            $this->data['ages'] = $result[0]['ages'];
            $this->data['contents'] = $result[0]['contents'];
            $this->data['years'] = $result[0]['years'];
            $this->data['id'] = $result[0]['id'];
            $this->data['image'] = $result[0]['image'];

            $this->data['act_title'] = 'Thay đổi';
            $this->data['act'] = 'edit';
            return $this->view('admin/tuvi/form', $this->data);
        }
    }

    public function listTuvi() {
        $page = (isset($_GET['page']) && $_GET['page'] ) ? (int)$_GET['page'] : 1;
        $start = ($page - 1) * PER_PAGE; //offset
        $cm = new Common();

        $db = new HomeModel();
        $strSearch = '';
        if (isset($_POST['search'])) {
            $strSearch = $_POST['search'];
        }elseif (isset($_GET['page'])) {
            $strSearch = $_SESSION['strSearch'];
        }
        $_SESSION['strSearch'] = $strSearch;

        $list = $db->listData($start, PER_PAGE, $_SESSION['strSearch']);
        $total = $db->countData($_SESSION['strSearch']);

        $common = new Common();
        $this->data['paging'] = $common->paging($total, $page, $this->data['url_current_ad'],PER_PAGE);
        $this->data['list'] = $list;
        $this->data['strSearch'] = $_SESSION['strSearch'];

        $this->data['act_title'] = 'Danh sách';
        return $this->view('admin/tuvi/list', $this->data);
    }

    public function delete() {
        $db = new HomeModel();
        $id = $_GET['id'];
        $result = $db->delete($id);
        if ($result == true) {
            $this->data['notice'] = 'success';
            $this->data['act_insert'] = 'Xóa thành công !';
        } else {
            $this->data['notice'] = 'error';
            $this->data['act_insert'] = 'Xóa không thành công !';
        }

        $this->listTuvi();
    }
}